<?php


namespace App\Repositories\Policy\Eloquent;


use Curl\CurlService\CurlService;
use Curl\StrService\DateService;
use Curl\StrService\StrService;
use Illuminate\Support\Facades\Cache;

class TcRepository
{
    //订单部分
    protected $httpsBaseUrl     = 'https://flightopenapi.17u.cn';
    protected $app_count        = 'hnhb';
    protected $app_id           = '76345508';
    protected $app_key          = 'hnhb';
    protected $app_secret       = 'hnhb@tcjp111';

    //政策部分
    protected $policyBaseUrl    = 'http://servicegw.ly.com/gateway/supplierapi/product/';
    protected $header           =  ['Labrador-Token:b9796d5e-636a-4528-9dc2-8f6aba405c18','Content-type:application/json'];
    protected $messageHead      = [];
    protected $messageBody      = [];

    public function __construct(){

        $time = DateService::mtime();
        $this->messageHead["messageHead"]=[
            "userName"=>"hnhb",
            "requestGuid"=>StrService::uuid(),
            "requestTimeStamp"=>$time,
            "signature"=>strtoupper(md5("hnhb#hnhb@tcjp111#{$time}"))
        ];
    }

    /**
     * 数据请求
     * @param $url
     * @param array $param
     * @return mixed
     */
    public function req($url,$param = []){

        //$param['messageHead']['requestGuid'] = StrService::uuid();
        return $this->curlRequest($this->policyBaseUrl.$url,$param);
    }

    /**
     * 返回同城订单请求
     * @param $url
     * @param array $param
     * @return mixed
     */
    public function httpsRequest($url,$param = []) {
        return $this->curlRequest($this->httpsBaseUrl.$url,$param);
    }

    /**
     * 数据查询
     * @param array $param
     * @return mixed
     */
    private function curlRequest($url,$param = []){
        $res = CurlService::post($url,$param,$this->header);
        return $res;
    }


    /**
     * 获取token
     * @return mixed|null
     */
    public function token()
    {

        Cache::set('tc_access_token','');
        $access_token = Cache::get('tc_access_token');
        //$access_token = null;
        if (!$access_token) {

            $time = time();

            $url = '/openapi/create/accesstoken?t='.$time;

            $param = [
                'app_id'    => $this->app_id,
                'app_key'   => $this->app_key
            ];

            $this->header = [
                'Content-Type:application/json',
                'x-tg-token:'.$time,
                'x-tg-sign:'.$this->sign($url,$param,$time),
            ];

            $res = $this->httpsRequest($url,$param);
            if ($res && !empty($res['access_token'])) {

                $access_token = $res['access_token'];
                Cache::put('tc_access_token',$res['access_token'],119);
            }
        }
        return $access_token;
    }

    /**
     * sign签名方式
     * @param $url
     * @param $body
     * @param $time
     * @return string
     */
    public function sign($url,$body,$token){


        $body = json_encode($body,JSON_UNESCAPED_UNICODE);
        $hashKey = $this->app_secret.$token;
        return hash_hmac('sha256',$url.$body,$hashKey);
    }

    /**
     * 设置请求Header
     * @param $url
     * @param $param
     * @return void
     */
    public function setHeader($url,$param){

        $token = $this->token();
        $this->header = [
            'Content-Type:application/json',
            'x-tg-token:'.$token,
            'x-tg-sign:'.$this->sign($url,$param,$token),
            'x-appkey:'.$this->app_key
        ];

    }
}